package frame;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.User;
import singleton.SingleRegisterFrame;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import utils.MybatisUtil;

public class LoginFrame extends JFrame {
    JTextField userid=new JTextField(12);
    JPasswordField userpwd=new JPasswordField(12);
    JLabel welcome = new JLabel("欢迎登陆本系统");
    JButton blog=new JButton("登录");
    JButton breg=new JButton("注册");
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel();
    static String id;

    // 获取SqlSession对象
    private SqlSession sqlSession = MybatisUtil.getSession();
    // 创建userDao接口，然后通过反射来获取代理对象
    private UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    //构造无参构造器把主要的方法放在构造器里,然后在main方法里面调
    public LoginFrame(){
        setTitle("登录界面");
        setBounds(400, 200, 400, 270);
        Container c = getContentPane();
        c.setLayout(new GridLayout(6,1));
        welcome.setFont(new Font("宋体", Font.BOLD, 20));
        p1.add(welcome);
        c.add(p1);
        p2.add(new JLabel("用户名"));
        p2.add(userid);
        c.add(p2);
        p3.add(new JLabel("密码"));
        p3.add(userpwd);
        c.add(p3);
        p4.add(blog);
        c.add(p4);
        p4.add(breg);
        c.add(p4);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null);

        //注意：此处是匿名内部类
        //实现注册
        breg.addActionListener(new ActionListener(){
                                 public void actionPerformed(ActionEvent e) {
                                     // TODO Auto-generated method stub
                                     // 单例模式生成一个注册界面
                                     SingleRegisterFrame.getInstance();
                                 }
                             }
        );
        //注意：此处是匿名内部类
        //实现登陆
        blog.addActionListener(new ActionListener() {
                                   public void actionPerformed(ActionEvent e) {
                                       // TODO Auto-generated method stub
                                       id = userid.getText();
                                       String pwd = userpwd.getText();
                                       if (id == null || pwd == null || id.equals("") || pwd.equals("")) {
                                           int res = JOptionPane.showConfirmDialog(null, "是否继续登陆", "输入不能为空",
                                                   JOptionPane.YES_NO_OPTION);
                                           if (res == JOptionPane.YES_OPTION) {
                                               System.out.println("继续登陆");
                                           } else {
                                               System.out.println("退出登录");
                                               System.exit(0);
                                           }
                                       } else {
                                           int cnt = userMapper.selectHasUser(id);
                                           if (cnt == 0) {
                                               int res = JOptionPane.showConfirmDialog(null, "请先注册", "用户不存在",
                                                       JOptionPane.YES_NO_OPTION);
                                               if (res == JOptionPane.YES_OPTION) {
                                                   System.out.println("继续登陆");
                                               } else {
                                                   System.out.println("登录失败");
                                               }
                                           } else {
                                               User user = userMapper.findUserById(id);
                                               String mypwd = user.getUserpwd();
                                               if (mypwd.equals(pwd)) {
                                                   // 跳转到主界面
                                                   JOptionPane.showMessageDialog(null, "登陆成功");
                                                   //切换页面
                                                   new MainFrame().setVisible(true);
                                                   // 关闭登录窗口，并且关闭会话资源
                                                   sqlSession.close();
                                                   setVisible(false);
                                               } else {
                                                   JOptionPane.showMessageDialog(null, "登陆失败，请检查用户名和密码");
                                                   userid.setText("");
                                                   userpwd.setText("");
                                               }
                                           }
                                       }
                                   }
                               }
        );
                                 }
}
